﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows ;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using System.Drawing;


namespace FBDMaker
{
    public class FolderTreeInf
    {
        
        public FolderTreeInf(string fname , string fpath ,ImageSource img=null, Boolean ischeck=false)
        {
            FName = fname;
            FPath = fpath;
            FType = FBDMaker.Properties.Resources.Dir;
            IsChecked = ischeck;
            Img = img;
            IsFolder = true;
            Node = new List<FolderTreeInf>();
            LibInfo = new FBDInf();
        }

        public FolderTreeInf()
        {
            FName = string.Empty;
            FPath = string.Empty;
            FType = string.Empty;
            IsChecked = false; ;
            IsFolder = true;
            Node = new List<FolderTreeInf>();
            Img = null;
            LibInfo = new FBDInf();
 
        }

        public FolderTreeInf(FileSystemInfo fi, Boolean ischeck = false)
        {
            FName = fi.Name;
            FPath = fi.FullName;
            FType = fi.Attributes==FileAttributes.Directory ?  FBDMaker.Properties.Resources.Dir : fi.Extension;
            IsChecked = ischeck;
            IsFolder =fi.Attributes==FileAttributes.Directory ? true:  false;
            LibInfo = new FBDInf();
            Img = ShellIcon.ImageSourceLargeIcon(FPath); //ShellIcon.ImageSource4Icon(Icon.ExtractAssociatedIcon(fi.FullName));
            Node = new List<FolderTreeInf>();
 
        }
        public string FName { get; set; }
        public string FPath {get;set;}
        public string FType { get; set; }
        public Boolean IsChecked {get;set;}
        public List<FolderTreeInf> Node { get; private set; }
        public ImageSource Img { get; set; }
        public Boolean IsFolder { get;private set; }
 
        public FBDInf LibInfo { get; private set; }

        public Boolean HasChaildFolder
        {
            get
            {
                if (!IsFolder)
                    return false;
                try
                {
                    string[] root = Directory.GetDirectories(FPath);
                    if (root.Length > 0)
                        return true;
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        }
        public Boolean HasChaildFile
        {
            get
            {
                if (!IsFolder)
                    return false;
                try
                {
                    string[] root = Directory.GetFiles(FPath);
                    if (root.Length > 0)
                        return true;
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        }
        public Boolean HasChaild
        {
            get
            {
                if (!IsFolder)
                    return false;
                if (HasChaildFile || HasChaildFolder)
                    return true;
                else
                    return false;
            }
        }

        public static void GetTopItem(ICollection<FolderTreeInf> ListNode) //<ExplorerNode>
        {
            //List<TreeListNode> ListNode = new List<TreeListNode>();
            FolderTreeInf FDT = new FolderTreeInf(FBDMaker.Properties.Resources.Desktop, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
            //TreeListNode DT = new TreeListNode() { Content = FDT };
            //FDT.Node=DT;
            FDT.Img=ShellIcon.ImageSourceLargeIcon(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
            if (FDT.HasChaildFolder)
            {
                FDT.Node.Add(new FolderTreeInf());
            }
            ListNode.Add(FDT);

            FolderTreeInf FMD=new FolderTreeInf(FBDMaker.Properties.Resources.MyDoc, Environment.GetFolderPath(Environment.SpecialFolder.Personal));
            FMD.Img = ShellIcon.ImageSourceLargeIcon(FMD.FPath);
            if (FMD.HasChaildFolder)
            {
                FMD.Node.Add(new FolderTreeInf());
            }
            ListNode.Add(FMD);
            String s = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";
            if (Directory.Exists(s))
            {
                
                FolderTreeInf FDD=new FolderTreeInf(FBDMaker.Properties.Resources.Downloads,s);
                FDD.Img = ShellIcon.ImageSourceLargeIcon(FDD.FPath);
                if (FDD.HasChaildFolder)
                {
                    FDD.Node.Add(new FolderTreeInf());
                }
                ListNode.Add(FDD);
            }
            String[] ld = Directory.GetLogicalDrives();
            foreach (string sd in ld)
            {
                FolderTreeInf FLD=new FolderTreeInf(sd,sd);
                FLD.Img = ShellIcon.ImageSourceLargeIcon(FLD.FPath);
                if (FLD.HasChaildFolder)
                {
                    FLD.Node.Add(new FolderTreeInf());
                }
                ListNode.Add(FLD);
            }
           // return ListNode;
        }


        public static FolderTreeInf GetFillNode(FileSystemInfo di, Boolean FoldersOnly = true)
        {
            FolderTreeInf FLD = new FolderTreeInf(di);
            //TreeListNode LD = new TreeListNode(FLD);
            //FLD.Node = LD;
            if (FoldersOnly)
            {
                if (FLD.HasChaildFolder)
                    FLD.Node.Add(new FolderTreeInf());
            }
            else
                if (FLD.HasChaild)
                {
                    FLD.Node.Add(new FolderTreeInf());
                }
             
            return FLD;

        }

        public static void GetListItem(ICollection<FolderTreeInf> ListNode, String folderpath)
        {
            GetListFolders(ListNode,folderpath,false);
            GetListFiels(ListNode,folderpath);
           
        }


        public static void GetListFolders(ICollection<FolderTreeInf> ListNode, String folderpath, Boolean FoldersOnly = true)
        {
          
            DirectoryInfo di;
            ListNode.Clear();
            try
            {
                string[] root = Directory.GetDirectories(folderpath);
                foreach (string s in root)
                {
                    try
                    {
                        di = new DirectoryInfo(s);
                        FolderTreeInf LD = GetFillNode(di, FoldersOnly);
                        
                        ListNode.Add(LD);
                    }
                    catch { }
                }
            }
            catch { }
           // return ListNode;
        }


        public static void GetListFiels(ICollection<FolderTreeInf> ListNode, String folderpath)
        {
            //List<TreeListNode> ListNode = new List<TreeListNode>();
            
            FileInfo di;
            try
            {
                string[] root = Directory.GetFiles(folderpath);
                foreach (string s in root)
                {
                    try
                    {
                        di = new FileInfo(s);
                        FolderTreeInf LD = GetFillNode(di);
                        
                        ListNode.Add(LD);
                    }
                    catch { }
                }
            }
            catch { }
            ///return ListNode;
        }

        public void OpenFolders(Boolean FoldersOnly=true)
        {

            GetListFolders(Node, FPath, FoldersOnly);
            
        }

        public void OpenFiles()
        {
            GetListFiels(Node, FPath);
            
        }

        public void OpenNode(Boolean FoldersOnly = true)
        {
            Node.Clear();
            OpenFolders(FoldersOnly);
            if (!FoldersOnly)
                OpenFiles();
        }
    }
}
